﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using AjaxControlToolkit;
using RapidWebDev.Platform;
using RapidWebDev.Common;
using System.Xml;
using System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace RapidWebDev.Web.Services
{
    /// <summary>
    /// Summary description for CarService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]    
    [System.Web.Script.Services.ScriptService]
    public class CarService : System.Web.Services.WebService
    {
        IHierarchyApi hierarchyApi = SpringContext.Current.GetObject<IHierarchyApi>();

        IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
              
        [WebMethod]
        public CascadingDropDownNameValue[] GetTestPlans(string knownCategoryValues, string category)
        {
            IList<CascadingDropDownNameValue> rets = new List<CascadingDropDownNameValue>();
            //get all testplan
            string parentId = string.Empty;
            if (!string.IsNullOrEmpty(authenticationContext.DynamicParameter))
                parentId = authenticationContext.DynamicParameter;
            else if (authenticationContext.Session != null)
            {
                if (authenticationContext.Session["CurrentProject"] != null)
                    parentId = authenticationContext.Session["CurrentProject"].ToString();
            }
            IEnumerable<HierarchyDataObject> plans = hierarchyApi.GetAllChildren("3", new Guid(parentId));
            foreach (var tmp in plans)
            {
                CascadingDropDownNameValue nv = new CascadingDropDownNameValue()
                {
                    name = tmp.Name,
                    value = tmp.Code
                };
                rets.Add(nv);
            }
            return rets.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] GetBuilds(string knownCategoryValues, string category)
        {

            IList<CascadingDropDownNameValue> rets = new List<CascadingDropDownNameValue>();

            StringDictionary knownCategoryValuesDictionary = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            //get all build
            IEnumerable<HierarchyDataObject> builds = hierarchyApi.GetAllChildren("4", new Guid(knownCategoryValuesDictionary["testplan"].ToString()));
            foreach (var tmp in builds)
            {
                CascadingDropDownNameValue nv = new CascadingDropDownNameValue()
                {
                    name = tmp.Name,
                    value = tmp.Code
                };
                rets.Add(nv);
            }
            return rets.ToArray();
        }
    }
}
